﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;

namespace Jy123.Web.AppCode
{
    public class Module : System.Web.UI.Page
    {
        public string BindHeader(string headerString, string[] strClass)
        {
            StringBuilder headerHTML = new StringBuilder();
            headerHTML.AppendFormat(headerString, strClass[0], strClass[1], strClass[2], strClass[3]);
            return headerHTML.ToString();
        }

        public string BindLeftMenu(string leftMenuHTML, string stringLi, string[] strContent, string[] strHref, string[] strCss)
        {
            StringBuilder leftMenuHTML_li = new StringBuilder();
            for (int i = 0; i < strContent.Length; i++)
            {
                string strLi = stringLi;
                strLi = strLi.Replace("$LeftMenuLiHref$", strHref[i]);
                strLi = strLi.Replace("$LeftMenuLiClass$", strCss[i]);
                strLi = strLi.Replace("$LeftMenuLiName$", strContent[i]);
                leftMenuHTML_li.Append(strLi);
            }
            leftMenuHTML = leftMenuHTML.Replace("$LeftMenuLi$", leftMenuHTML_li.ToString());
            return leftMenuHTML;
        }

        public string BindContentClub(string contentClubHTML, Jy123.Model.Module_Main module, Jy123.Model.Venues_Main venues, Jy123.Model.User_Main user, Jy123.Model.Public_Level level)
        {
            contentClubHTML = contentClubHTML.Replace("$FBUserName$", user.UserName);
            contentClubHTML = contentClubHTML.Replace("$FBVenuesName$", venues.VName);
            contentClubHTML = contentClubHTML.Replace("$FBVenuesLevel$", level.LName);
            string strStatus = string.Empty;
            switch (venues.VStatus.Value)
            {
                case 0:
                    strStatus = "空闲";
                    break;
                case 1:
                    strStatus = "等待";
                    break;
                case 2:
                    strStatus = "比赛";
                    break;
                case 3:
                    strStatus = "维护";
                    break;
                case 4:
                    strStatus = "租用";
                    break;
            }
            contentClubHTML = contentClubHTML.Replace("$FBVenuesStatus$", strStatus);
            int raceTimes = 0;
            if (venues.RaceTimes.HasValue)
            {
                raceTimes = venues.RaceTimes.Value;
            }
            contentClubHTML = contentClubHTML.Replace("$FBVenuesTimes$", raceTimes.ToString());
            contentClubHTML = contentClubHTML.Replace("$FBVenuesRaceType$", "待定");
            contentClubHTML = contentClubHTML.Replace("$FBVenuesAssets$", (Convert.ToInt32(venues.Assets.Value)).ToString());
            contentClubHTML = contentClubHTML.Replace("$FBVenuesCash$", (Convert.ToInt32(user.Cash.Value)).ToString());
            contentClubHTML = contentClubHTML.Replace("$Times$", "比赛次数");
            return contentClubHTML;
        }

        public string BindContentClub(string contentClubHTML, Jy123.Model.Module_Main module, Jy123.Model.TCenter_Main tcenter, Jy123.Model.User_Main user, Jy123.Model.Public_Level level)
        {
            contentClubHTML = contentClubHTML.Replace("$FBUserName$", user.UserName);
            contentClubHTML = contentClubHTML.Replace("$FBVenuesName$", tcenter.TName);
            contentClubHTML = contentClubHTML.Replace("$FBVenuesLevel$", level.LName);
            string strStatus = string.Empty;
            switch (module.MStatus.Value)
            {
                case 0:
                    strStatus = "空闲";
                    break;
                case 1:
                    strStatus = "训练中";
                    break;
                case 2:
                    strStatus = "满员";
                    break;
                case 3:
                    strStatus = "维护";
                    break;
                case 4:
                    strStatus = "租用";
                    break;
            }
            contentClubHTML = contentClubHTML.Replace("$FBVenuesStatus$", strStatus);
            
            contentClubHTML = contentClubHTML.Replace("$FBVenuesTimes$", tcenter.UsingTimes.ToString());
            contentClubHTML = contentClubHTML.Replace("$FBVenuesRaceType$", "待定");
            contentClubHTML = contentClubHTML.Replace("$FBVenuesAssets$", (Convert.ToInt32(tcenter.Assets.Value)).ToString());
            contentClubHTML = contentClubHTML.Replace("$FBVenuesCash$", (Convert.ToInt32(user.Cash.Value)).ToString());
            contentClubHTML = contentClubHTML.Replace("$Times$", "使用次数");
            return contentClubHTML;
        }

        public string BindMainMenu(string strMain, string strSubDiv, string strLiDiv, int userId)
        {
            Jy123.BLL.Public_JType BLLJTYpe = new Jy123.BLL.Public_JType();
            Jy123.BLL.Venues_Main BLLVenues = new Jy123.BLL.Venues_Main();
            Jy123.BLL.Public_SportType BLLSportType = new Jy123.BLL.Public_SportType();
            List<Jy123.Model.Public_JType> listAllJType = BLLJTYpe.GetModelList("JFather IS NOT NULL");
            List<Jy123.Model.Venues_Main> listVenues = BLLVenues.GetModelList("FUser = " + userId.ToString());
            Dictionary<int, Jy123.Model.Public_JType> dicJTypes = new Dictionary<int, Jy123.Model.Public_JType>();
            List<Jy123.Model.Public_SportType> listSportType = BLLSportType.GetModelList(string.Empty);
            Dictionary<int, Jy123.Model.Public_SportType> dicSportType = new Dictionary<int, Jy123.Model.Public_SportType>();
            foreach (Jy123.Model.Public_JType JType in listAllJType)
            {
                dicJTypes.Add(JType.ID, JType);
            }
            foreach (Jy123.Model.Public_SportType sportType in listSportType)
            {
                dicSportType.Add(sportType.ID, sportType);
            }
            string strSub = BuildSubDivString(strSubDiv, strLiDiv, dicJTypes, userId, dicSportType);
            strMain = strMain.Replace("$MainMenuSubDiv$", strSub);
            Jy123.BLL.User_Main BLLUser = new Jy123.BLL.User_Main();
            Jy123.Model.User_Main user = BLLUser.GetModel(userId);
            strMain = strMain.Replace("$UserName$", user.UserName);
            strMain = strMain.Replace("$UserId$", user.ID.ToString());
            return strMain;
        }

        private string BuildSubDivString(string strSubDiv, string strLiDiv, Dictionary<int, Jy123.Model.Public_JType> dicJTypes, int userId, Dictionary<int, Jy123.Model.Public_SportType> dicSportType)
        {
            StringBuilder strBuilderSub = new StringBuilder();
            string strSub = strSubDiv;
            string strSubClub = strSubDiv;
            StringBuilder strSubLi = new StringBuilder();
            StringBuilder strSubLiClub = new StringBuilder();
            string strWhere = string.Format("FUser = {0}", userId);
            string strBlock = "display:block";
            string strNone = "display:none";
            Jy123.BLL.Venues_Main BLLVenues = new Jy123.BLL.Venues_Main();
            Jy123.BLL.Club_Main BLLClub = new Jy123.BLL.Club_Main();
            BLL.TCenter_Main BLLTCenter = new Jy123.BLL.TCenter_Main();
            List<Jy123.Model.Venues_Main> listVenues = BLLVenues.GetModelList(strWhere);
            List<Jy123.Model.Club_Main> listClub = BLLClub.GetModelList(strWhere);
            List<Jy123.Model.TCenter_Main> listTCenter = BLLTCenter.GetModelList(strWhere);
            foreach (Jy123.Model.Venues_Main venues in listVenues)
            {
                string strLi = strLiDiv;
                Jy123.Model.Public_JType JType = dicJTypes[venues.FJType.Value];
                strLi = strLi.Replace("$VenuesHref$", "/Module/Venues/MyVenues.aspx?VenuesId=" + venues.ID);
                strLi = strLi.Replace("$VenuesName$", JType.JName);
                string strStyle = string.Format("background: url({0}) bottom left no-repeat;", JType.Icon);
                strLi = strLi.Replace("$VenuesStyle$", strStyle);
                strSubLi.Append(strLi);
            }
            foreach (Jy123.Model.Club_Main club in listClub)
            {
                string strLi = strLiDiv;
                Jy123.Model.Public_JType JType = dicJTypes[club.FJType.Value];
                if (JType.FSportType.Value == 13 || JType.FSportType.Value == 14)
                {
                    strLi = strLi.Replace("$VenuesHref$", "/Module/CarClub/MyClub.aspx?ClubId=" + club.ID);
                }
                else
                {
                    strLi = strLi.Replace("$VenuesHref$", "/Module/Club/MyClub.aspx?ClubId=" + club.ID);
                }

                strLi = strLi.Replace("$VenuesName$", JType.JName);
                string strStyle = string.Format("background: url({0}) bottom left no-repeat;", JType.Icon);
                strLi = strLi.Replace("$VenuesStyle$", strStyle);
                strSubLiClub.Append(strLi);
            }
            foreach (Jy123.Model.TCenter_Main TCenter in listTCenter)
            {
                string strLi = strLiDiv;
                BLL.Module_Main BLLModule = new Jy123.BLL.Module_Main();
                Model.Module_Main module = BLLModule.GetModel(TCenter.ID);
                Jy123.Model.Public_JType JType = dicJTypes[module.FJType.Value];                
                strLi = strLi.Replace("$VenuesName$", JType.JName);
                strLi = strLi.Replace("$VenuesHref$", "/Module/TCenter/MyTCenter.aspx?TCenterId=" + TCenter.ID);
                string strStyle = string.Format("background: url({0}) bottom left no-repeat;", JType.Icon);
                strLi = strLi.Replace("$VenuesStyle$", strStyle);
                strSubLi.Append(strLi);
            }
            strSub = strSub.Replace("$MainMenuSubDivLi$", strSubLi.ToString());
            strSub = strSub.Replace("$DivId$", "menuDiv1");
            strSub = strSub.Replace("$style$", strBlock);

            strSubClub = strSubClub.Replace("$MainMenuSubDivLi$", strSubLiClub.ToString());
            strSubClub = strSubClub.Replace("$DivId$", "menuDiv2");
            strSubClub = strSubClub.Replace("$style$", strNone);
            strBuilderSub.Append(strSub.ToString());
            strBuilderSub.Append(strSubClub.ToString());
            return strBuilderSub.ToString();
        }

        public string BindClubList(string str, int userId, List<Jy123.Model.Club_Main> listFriendClub)
        {
            StringBuilder strClubList = new StringBuilder();
            Jy123.BLL.User_Main BLLUser = new Jy123.BLL.User_Main();
            string strWhere = string.Format("ID IN (SELECT FFriend FROM User_Friend WHERE FUser = {0})", userId);
            List<Jy123.Model.User_Main> listFriends = BLLUser.GetModelList(strWhere);
            Dictionary<int, Jy123.Model.User_Main> dicFriend = new Dictionary<int, Jy123.Model.User_Main>();
            foreach (Jy123.Model.User_Main user in listFriends)
            {
                dicFriend.Add(user.ID, user);
            }
            BLL.Venues_Main BLLVenues = new Jy123.BLL.Venues_Main();
            string strVWhere = "ID IN (SELECT FVenues FROM Public_Game WHERE IsEnd = 0)";
            List<Model.Venues_Main> listVenues = BLLVenues.GetModelList(strVWhere);
            int cVId = 0;
            for (int i = 0; i < listFriendClub.Count; i++)
            {
                Jy123.Model.Club_Main club = listFriendClub[i];
                string strClub = str;
                strClub = strClub.Replace("$ClubCheckBoxId$", "checkbox" + club.ID.ToString());
                strClub = strClub.Replace("$Value$", club.ID.ToString());
                strClub = strClub.Replace("$ClubUserName$", dicFriend[club.FUser.Value].UserName);
                string strCName = club.CName;
                if (strCName.Length > 5)
                {
                    strCName = strCName.Substring(0, 5) + "...";
                }
                strClub = strClub.Replace("$ClubName$", strCName);
                strClub = strClub.Replace("$ClubWinRate$", club.Rate.ToString() + "%");
                string strChkStyle = string.Empty;
                string strAStyle = string.Empty;
                string strImgUrl = "/Images/Module/empty.gif";
                string strAlt = "空闲";
                string strHref = string.Empty;
                if (club.CStatus.Value == Convert.ToInt32(Jy123.BLL.Public.EnumConfig.ClubStatus.Empty))
                {
                    strChkStyle = "<input name=" + "checkbox" + club.ID.ToString() + " id=" + "checkbox" + club.ID.ToString() + " type='checkbox' value=" + club.ID.ToString() + " />";
                }
                else if (club.CStatus.Value == Convert.ToInt32(Jy123.BLL.Public.EnumConfig.ClubStatus.Race))
                {
                    int cClubId = club.ID;
                    strImgUrl = "/Images/Module/race.gif";
                    strAlt = "比赛";

                    foreach (Model.Venues_Main cVenues in listVenues)
                    {
                        if (!string.IsNullOrEmpty(cVenues.RaceClub))
                        {
                            string[] strList = cVenues.RaceClub.Split(',');
                            if (IsInList(strList, cClubId))
                            {
                                cVId = cVenues.ID;
                            }
                        }
                    }
                    strAStyle = "<a href='/Module/Venues/VenuesRace.aspx?VenuesId=" + cVId.ToString() + "'><img src='/Images/Module/cheer.gif' alt='给他加油' /></a>";
                    //strAStyle = "<a href='/Module/Venues/VenuesRace.aspx?VenuesId=2'><img src='/Images/Module/cheer.gif' alt='给他加油' /></a>";
                }
                strClub = strClub.Replace("$ChcekBox$", strChkStyle);
                strClub = strClub.Replace("$A$", strAStyle);
                strClub = strClub.Replace("$SImgUrl$", strImgUrl);
                strClub = strClub.Replace("$SAlt$", strAlt);
                strClub = strClub.Replace("$Href$", strHref);
                strClubList.Append(strClub);
            }
            return strClubList.ToString();
        }

        private bool IsInList(string[] strList, int id)
        {
            for (int i = 0; i < strList.Length; i++)
            {
                if (strList[i].Equals(id.ToString()))
                {
                    return true;
                }
            }
            return false;
        }

        public string BindAnnexeString(string str, Jy123.Model.Public_Level level)
        {
            Jy123.BLL.Annexe.Annexe BLLAnnexe = new Jy123.BLL.Annexe.Annexe();
            List<Jy123.Model.Public_Annexe> listAnnexe = BLLAnnexe.AnnexeListCanUse(level);
            StringBuilder currentString = new StringBuilder();
            foreach (Jy123.Model.Public_Annexe annexe in listAnnexe)
            {
                string strLi = str;
                strLi = strLi.Replace("$LiContent$", annexe.AName);
                currentString.Append(strLi);
            }
            return currentString.ToString();
        }
    }
}
